{extend name="base"}
{block name="title"}
点赚金-后台管理-配送员管理
{/block}
{block name="pageName"}
配送员列表
{/block}
{block name="pageBody"}
<!-- 一个表格 -->
<div class="btn-toolbar">
    <button class="btn btn-primary" data-title="Add Courier" data-toggle="modal" data-target="#add_courier"><i class="icon-plus"></i>添加配送员</button>
    <button class="btn btn-success" onclick="javascript:location.reload()">刷新</button>
  <div class="btn-group">
  </div>
</div>
<div class="well">
    <table class="table">
      <thead>
        <tr>
			<th>#</th>
			<th>配送员名称</th>
			<th>联系电话</th>
			<th>正在配件数量</th>
			<th>注册时间</th>
			<th>状态</th>
			<th>操作</th>
          	<th style="width: 26px;"></th>
        </tr>
      </thead>
      <tbody id="couriers">
        {volist name='couriers' id='courier'}
		<tr id="{$courier.courier_id}">
			<th scope="row">{$i}</th>
			<td>{$courier.courier_name}</td>
			<td>{$courier.courier_phone}</td>
			<td>{$courier.num}</td>
			<td>{$courier.gmt_create}</td>
			<td>
				{if condition="$courier.status eq 0"}
					<span class="badge badge-success">正常</span>
					{else/}
					<span class="badge badge-danger">禁用</span>
				{/if}		
			</td>
			<td>
				<div class="dropdown">
					<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="false">操作<span class="caret"></span></button>
					<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu" x-placement="bottom-start" style="position: absolute; transform: translate3d(0px, 38px, 0px); top: 0px; left: 0px; will-change: transform;">
						<li>{if condition="$courier.status eq 0"}
							<a onclick="disable('{$courier.courier_id}')">禁用</a>
							{else/}
							<a onclick="enable('{$courier.courier_id}')">启用</a>
						{/if}</li>
						<li><a onclick="resetPassword('{$courier.courier_id}')">重置密码</a></li>
						<!-- <li><a onclick="deleteUser('{$courier.courier_id}')">删除</a></li> -->
					</ul>
				</div>
			</td>
		</tr>
		{/volist}
      </tbody>
    </table>
</div>
<div class="pagination">
    {$couriers->render()}
</div>

<!-- 进行状态修改的jQuery函数 -->
	<script>
		function disable(courier_id){
			$.getJSON("{:url('unenableCourier')}",{courier_id:courier_id},function(data){
				data = data.data;
					$("#"+data.courier_id+" td").eq(4).html('<span class="badge badge-danger">禁用</span>');
					$("#"+data.courier_id+" ul li").eq(0).html('<a class="dropdown-item" onclick="enable(\''+data.courier_id+'\')">启用</a>');
			})	
		}
		function enable(courier_id){
			$.getJSON("{:url('enableCourier')}",{courier_id:courier_id},function(data){
				data = data.data;
					$("#"+data.courier_id+" td").eq(4).html('<span class="badge badge-success">正常</span>');
					$("#"+data.courier_id+" ul li").eq(0).html('<a class="dropdown-item" onclick="disable(\''+data.courier_id+'\')">禁用</a>');
			})
		}
		function deleteUser(user_id){
			if(confirm("确认删除？")){
				$.getJSON("{:url('douserdele')}",{user_id:user_id},function(data){
					if(0 == data.code){
						$("#"+user_id).remove();
						alert("用户编号:"+data.data+"删除成功");
					}else{
						alert("删除失败");
					}
					
				});
			}
		}
		function resetPassword(courier_id){
			if(confirm("确认重置该配送员密码为 “ 123456 ” ？")){
				$.getJSON("{:url('resetPassword')}",{courier_id:courier_id},function(data){
					if(0 == data.code){
						alert("管理员编号:"+data.data+"密码重置成功");
					}else{
						alert("密码重置失败");
					}
					
				});
			}
		}
	</script>
{/block}
<!-- 一个表格结束 -->

{block name="model"}
	<script>
		function addCourier(){
			var form = new FormData(document.getElementById("addcourier"));
			$.ajax({
                url:"{:url('addCourier')}",
                type:"post",
                data:form,
                processData: false,                // jQuery不要去处理发送的数据
    			contentType: false,
    			dataType: 'json',
                success:function(data){
                    if (0 == data.code) {
                    	var courier = data.data;
                    	var html = "<tr>";
                    		html += "<td>-</td>";
	                    	html += "<td>"+courier["courier_name"]+"</td>";
	                    	html += "<td>"+courier["courier_phone"]+"</td>";
	                    	html += "<td>0</td>";
	                    	html += "<td>刚刚</td>";	    
	                    	html += "<td><span class='badge badge-success'>正常</span></td>";
	                    	html += "<td>-</td>";
                    	html+="</tr>";
                    	$("#couriers").prepend(html);
                    	$('#add_courier').modal('hide');
                    }else{
                    	alert(data.msg);
                    }
                }
            }); 
		}
	</script>
	<div id="add_courier" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">添加配送员</h3>
		</div>
		<form id="addcourier" class="form-horizontal" onsubmit="return false;" method="post">
			<br>
			<div class="control-group">
	            <label class="control-label">配送员名称</label>
	            <div class="controls">
	              <input type="text" name="courier_name" placeholder="请输入用户姓名" required><br>
	        	</div>
	        </div>
	        <div class="control-group">
	            <label class="control-label">密码</label>
	            <div class="controls">
	              <input type="password" name="courier_password" placeholder="请输入密码" required><br>
	        	</div>
	        </div>
			<div class="control-group">
	            <label class="control-label">联系方式</label>
	            <div class="controls">
	              <input type="text" name="courier_phone" placeholder="请输入联系方式" required><br>
	        	</div>
	        </div>
		</form>
		<div class="modal-footer">
			<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
			<input class="btn" type="button" value="提交" onclick="addCourier();">
		</div>
	</div>
{/block}